using UnityEngine;
using UnityEngine.UI;
namespace DefaultNamespace.FaceCustom
{
    public class CharacterCustomizer : MonoBehaviour
    {
        [Header("捏脸设置")]
        public SkinnedMeshRenderer faceMesh; // 含 BlendShape 的脸部模型
        public Slider noseSlider;
        public Slider jawSlider;
        
        [Header("换装设置")]
        public Transform equipmentSlot; // 装备的父节点
        public Transform boneRoot; // 角色骨架根节点
        public GameObject[] clothingPrefabs; // 可供切换的服装
        
        private GameObject currentClothing;
        
        void Start()
        {
            // 初始化滑杆事件
            noseSlider.onValueChanged.AddListener(value => SetBlendShape("Nose_Size", value));
            jawSlider.onValueChanged.AddListener(value => SetBlendShape("Jaw_Width", value));
        }
        
        public void SetBlendShape(string shapeName, float value)
        {
            int index = faceMesh.sharedMesh.GetBlendShapeIndex(shapeName);
            if (index >= 0)
            {
                faceMesh.SetBlendShapeWeight(index, value);
            }
        }
        
        public void EquipClothing(int index)
        {
            if (currentClothing != null)
            {
                Destroy(currentClothing);
            }
            
            GameObject newClothing = Instantiate(clothingPrefabs[index], equipmentSlot);
            SkinnedMeshRenderer smr = newClothing.GetComponent<SkinnedMeshRenderer>();
            smr.rootBone = boneRoot;
            
            // 获取当前身体骨骼列表
            SkinnedMeshRenderer bodySMR = GetComponentInChildren<SkinnedMeshRenderer>();
            smr.bones = bodySMR.bones;
            
            currentClothing = newClothing;
        }
        public SkinnedMeshRenderer headRenderer; // 头部模型
        public SkinnedMeshRenderer bodyRenderer; // 身体模型
        public SkinnedMeshRenderer legsRenderer; // 腿部模型
        
        public void ChangePart(SkinnedMeshRenderer targetRenderer, SkinnedMeshRenderer newPart)
        {
            // 替换目标部件的模型和材质
            targetRenderer.sharedMesh = newPart.sharedMesh;
            targetRenderer.sharedMaterials = newPart.sharedMaterials;
        }
        
    }
}